<?php
namespace app\index\model;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 消息业务处理
 */
class Remind extends Base{
	/**
	 * 分页
	 */
	public function pageQuery(){
		$where = [];
		$where['dataFlag'] = 1;
		$user_name = input('get.user_name');
		$is_read = input('get.is_read');
		if($is_read != '')$where['is_read'] = $is_read;
		if(!empty($user_name))$where['user_name'] = ['like',"%$user_name%"];
		$where['type'] = session('remind_type');
        if(session("HTR_STAFF.staffRoleId")==3){
			$where['staffId'] = session("HTR_STAFF.staffId");        	
        }
        if($where['type']){
	        $where['remindTime'] = ['<=',date('Y-m-d')];
        }else{
	        $where['remindTime'] = ['<=',date('Y-m-d',strtotime("+1 day"))];
        }
		$rs = $this->where($where)->order('remindTime desc,remind_id desc')->paginate(input('pagesize/d'));
		if(!empty($rs)){
			foreach($rs as $k=>$v){
  				$rs[$k]['user_name'] = '<span class="read'.$v['is_read'].'">'.$v['user_name'].'</span>';
				$rs[$k]['desc'] = '<span class="read'.$v['is_read'].'">';
				if($v['type']){
					$rs[$k]['desc'] .= '客户'.$v['user_name'].'已'.$v['day'].'天未进行消费。</span>';
				}else{
					$rs[$k]['desc'] .= '明天是客户'.$v['user_name'].'的生日，快去为他送上祝福吧。</span>';
				}
			}
		}
		return $rs;
	}

	public function getById($id){
		return $this->get($id);
	}
	/**
	* 是否
	*/
	public function changeRead($id){
		$result = $this->update(['is_read'=>1],['remind_id'=>(int)$id]);
		if(false !== $result){
        	return HTRReturn("编辑成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}	
	/**
	 * 删除
	 */
	public function del(){
	    $id = input('post.id/d');
		$data = [];
		$data['dataFlag'] = -1;
	    $result = $this->update($data,['remind_id'=>$id]);
        if(false !== $result){
        	return HTRReturn("删除成功", 1);
        }
        return HTRReturn('删除失败');
	}
	/**
	 * 更新
	 */
	public function toUpdate(){
		$where = [];
		$where['dataFlag'] = 1;
		$where['is_read'] = 0;
        $where['remindTime'] = ['<=',date('Y-m-d',strtotime("+1 day"))];
		$rs = $this->where($where)->select();
		if(!empty($rs)){
			foreach($rs as $k=>$v){
				$user = model('users')->field('user_name,staffId')->where('user_id',$v['user_id'])->find();
				if($user)$this->where('remind_id',$v['remind_id'])->update(['user_name'=>$user['user_name'],'staffId'=>$user['staffId']]);
			}
		}
		return $rs;
	}
	/**
	 * 未读
	 */
	public function checkRead(){
		$where = [];
		$where['dataFlag'] = 1;
		$where['is_read'] = 0;
        if(session("HTR_STAFF.staffRoleId")==3){
			$where['staffId'] = session("HTR_STAFF.staffId");        	
        }        
        $where['type'] = 0;
        $where['remindTime'] = ['<=',date('Y-m-d',strtotime("+1 day"))];
        if($this->where($where)->count()){
			return 0;
        }
        $where['type'] = 1;
        $where['remindTime'] = ['<=',date('Y-m-d')];
        if($this->where($where)->count()){
			return 1;
        }
		return 2;
	}
}
